package com.cx.mall.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL) // NULL值不显示

public class ResponseVO<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;// 操作代码
    private String message; // 提示信息
    private T data;// 结果数据

    public ResponseVO(Integer code, String message) {
        this.code = code;
        this.message =message;
    }

    /**
     * 无数据的返回信息
     * @param responseEnum
     */
    public ResponseVO(ResponseEnum responseEnum) {
        this.code = responseEnum.getCode();
        this.message = responseEnum.getMessage();
    }

    /**
     * 带数据的返回信息
     * @param responseEnum
     * @param data
     */
    public ResponseVO(ResponseEnum responseEnum, T data) {
        this.code = responseEnum.getCode();
        this.message = responseEnum.getMessage();
        this.data = data;
    }

    /**
     * 成功返回-无数据
     * @return
     */
    public static ResponseVO<String> success() {
        return new ResponseVO<String>(ResponseEnum.SUCCESS);
    }
    /**
     * 成功返回封装-带数据
     * @param data
     * @return
     * @param <T>
     */
    public static <T> ResponseVO<T> success(T data) {
        return new ResponseVO<T>(ResponseEnum.SUCCESS, data);
    }
    //失败返回封装-使用默认提示信息
    public static ResponseVO<String> fail() {
        return new ResponseVO<String>(ResponseEnum.FAIL);
    }
    /**
     * 失败返回-自定义信息
     * @param code
     * @param message
     * @return
     * @param <T>
     */
    public static <T> ResponseVO<T> fail(Integer code,String message) {
        return new ResponseVO<T>(code, message);
    }
    /**
     * 失败返回
     * @param responseEnum
     * @return
     */
    public static ResponseVO<String> fail(ResponseEnum responseEnum) {
        return new ResponseVO<String>(responseEnum);
    }
    /**
     * 失败返回-带数据
     * @param responseEnum
     * @param data
     * @return
     * @param <T>
     */
    public static <T> ResponseVO<T> fail(ResponseEnum responseEnum,T data) {
        return new ResponseVO<T>(responseEnum, data);
    }
}
